package com.luoye.unionlotto;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class BuysUnionlottoFileReaderWriter {
    private String dataDir="";

    public BuysUnionlottoFileReaderWriter(String dataDir) {
        this.dataDir = dataDir;
    }

    public void write(String date,List<Unionlotto> unionlottoList){
        //写文件
        BufferedWriter bufferedWriter=null;
        try {
            File file=new File(this.dataDir+"/buys");
            if(!file.exists()||!file.isDirectory()){
                file.mkdirs();
            }

            bufferedWriter=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.dataDir+"/buys/"+date+".txt")));
            for(Unionlotto unionlotto:unionlottoList){
                //bufferedWriter.write(unionlotto.getDate()+":\t");
                for(String redBall:unionlotto.getRedBalls()){
                    bufferedWriter.write(redBall+"\t");
                }
                bufferedWriter.write("-\t"+unionlotto.blueBall);
                bufferedWriter.newLine();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(bufferedWriter!=null){
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public List<Unionlotto> read(String date){
        List<Unionlotto> unionlottoList=new ArrayList<Unionlotto>();
        //读文件
        BufferedReader bufferedReader=null;
        try {
            bufferedReader=new BufferedReader(new InputStreamReader(new FileInputStream(this.dataDir+"/buys/"+date+".txt")));
            String line=null;
            while ((line=bufferedReader.readLine())!=null){
                if(!line.equals("")) {
                    //String date = line.substring(0, line.indexOf(":\t"));
                    //String redBalls = line.substring(line.indexOf(":\t") + 2, line.indexOf("\t-\t"));
                    String redBalls = line.substring(0, line.indexOf("\t-\t"));
                    String blueBall = line.substring(line.indexOf("\t-\t") + 3);
                    Unionlotto unionlotto = new Unionlotto();
                    unionlotto.setDate(date);
                    List<String> redBallList = new ArrayList<String>();
                    redBallList.addAll(Arrays.asList(redBalls.split("\t")));
                    unionlotto.setRedBalls(redBallList);
                    unionlotto.setBlueBall(blueBall);
                    unionlottoList.add(unionlotto);
                }
            }
            System.out.println(unionlottoList);
            System.out.println(unionlottoList.size());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(bufferedReader!=null){
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return unionlottoList;
    }
}
